/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.fragment;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.BookmarkActivity;
import lt.marsrutai.mobile.android.activity.DonationsActivity;
import lt.marsrutai.mobile.android.activity.ScopesActivity;
import lt.marsrutai.mobile.android.activity.intent.FastestRoutesSearchIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.SchedulesIntentHolder;
import lt.marsrutai.mobile.android.model.MobileScope;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

public class ScopeNavigationFragment extends SherlockFragment {
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setupView();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.scope_navigation_layout, null);
        return view;
    }
    
    private void setupView() {
        setupSchedulesButton();
        setupStopSearchButton();
        setupFastestRoutesButton();
        setupBookmarksButton();
        setupDonationsButton();
    }

    private void setupSchedulesButton() {
        View schedulesButton = getActivity().findViewById(R.id.schedules);
        schedulesButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MobileScope mobileScope = getCurrentMobileScope();

                SchedulesIntentHolder holder = new SchedulesIntentHolder(ScopeNavigationFragment.this.getActivity());
                holder.setScopeFolder(mobileScope.getFolder());
                holder.setScopeName(mobileScope.getName());
                ScopeNavigationFragment.this.getActivity().startActivity(holder.getIntent());
            }
        });
    }

    private void setupStopSearchButton() {
        View stopSearchButton = getActivity().findViewById(R.id.stop_search);
        stopSearchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ScopeNavigationFragment.this.getActivity().onSearchRequested();
            }
        });
    }

    private void setupFastestRoutesButton() {
        View fastestRoutesButton = getActivity().findViewById(R.id.fastest_routes);
        fastestRoutesButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MobileScope mobileScope = getCurrentMobileScope();

                FastestRoutesSearchIntentHolder holder = new FastestRoutesSearchIntentHolder(ScopeNavigationFragment.this.getActivity());
                holder.setScopeFolder(mobileScope.getFolder());
                ScopeNavigationFragment.this.getActivity().startActivity(holder.getIntent());
            }
        });
    }

    private void setupBookmarksButton() {
        View bookmarksButton = getActivity().findViewById(R.id.bookmarks);
        bookmarksButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ScopeNavigationFragment.this.getActivity().startActivity(new Intent(ScopeNavigationFragment.this.getActivity(), BookmarkActivity.class));
            }
        });
    }
    
    private void setupDonationsButton() {
        View preferencesButton = getActivity().findViewById(R.id.donations);
        preferencesButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                ScopeNavigationFragment.this.getActivity().startActivity(new Intent(ScopeNavigationFragment.this.getActivity(), DonationsActivity.class));
            }
        });
    }

    public MobileScope getCurrentMobileScope() {
        return ((ScopesActivity) getActivity()).getCurrentMobileScope();
    }
}
